<?php
namespace app\api\controller;
use think\Controller;
use think\Request;
use app\api\model\FinanceModel;
use think\Db;
class Finance extends Common
{
    /**对账汇总----
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json
     */
    public function FinanceList(Request $request,FinanceModel $model)
    {
        $data =$request->param();
        $data['min'] =isset($data['min'])?$data['min']:"";
        $data['max'] =isset($data['max'])?$data['max']:"";
        $data['state'] =isset($data['state'])?$data['state']:0;
        $list = $model->getContrastList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    public function getSetAcount(FinanceModel $model)
    {
        return $this->returnJson(200,'成功',$model->getSetAcount());
    }

    public function getAllAcount(FinanceModel $model)
    {
        return $this->returnJson(200,'成功',$model->getAllAcount());
    }

    /**
     * 客户对账
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json
     */
    public function customer(Request $request,FinanceModel $model)
    {
        $data =$request->param();
        $data['start_time'] =isset($data['start_time']) ? $data['start_time'] : '';
        $data['end_time'] =isset($data['end_time']) ? $data['end_time'] : '';
        $data['number'] =isset($data['number']) ? $data['number'] : '';
        $list =$model->getCustomerList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }
    public function customerInfo($id,FinanceModel $model)
    {
        $list =$model->getCustomerInfo($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * 供应商对账
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json、
     */
    public function supplier(Request $request,FinanceModel $model)
    {
        $data =$request->param();
        $data['start_time'] =isset($data['start_time']) ? $data['start_time'] : '';
        $data['end_time'] =isset($data['end_time']) ? $data['end_time'] : '';
        $data['number'] =isset($data['number']) ? $data['number'] : '';
        $list =$model->getSupplierList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }
    public function supplierInfo($id,FinanceModel $model)
    {
        $list =$model->getSupplierInfo($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function collectionFlow(Request $request)
    {
        $where = [];
        // 操作人：
        if ($request->has('maker', 'param', true)) {
            $where[] = ['maker' , '=', $request->param('maker')];
        }
        //单号：
        if ($request->has('number', 'param', true)) {
            $where[] = ['number' , 'LIKE', '%'.$request->param('number').'%'];
        }
        // 支付类型：
        if ($request->has('payment_mode', 'param', true)) {
            $where[] = ['payment_mode' , '=', $request->param('payment_mode')];
        }
        //交易类型：
        if ($request->has('type1', 'param', true)) {
            $where[] = ['maker' , '=', $request->param('type1')];
        }
        // 操作人所属门店：
        if ($request->has('makerStore', 'param', true)) {
            $maker = Db::name('admin')->where('store', $request->param('makerStore'))->fetchCollection()->select();
            $where[] = ['maker' , 'IN', $maker->column('id')];
        }
        //日期：
        if($request->has('start_time', 'param', true) && $request->has('end_time', 'param', true)){
            $where[] =['create_time','between',[$request->param('start_time'),$request->param('end_time')]];
        }
        $res = Db::name('sale')->where($where)
            ->page($request->param('page', 1), $request->param('size', 10))
            ->paginate($request->param('size', 10));
        return $this->returnJson(200, 'ok', $res);
    }

    /**结算账户----列表
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json
     */
    public function setAccountList(Request $request,FinanceModel $model)
    {
        $list = $model->getAccountList();
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**结算账户----新增
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json
     */
    public function setAccountAdd(Request $request,FinanceModel $model)
    {
        $data =$request->param();
        $list = $model->getAccountAdd($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }
    /**结算账户----修改
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json
     */
    public function setAccountEdit($id,Request $request,FinanceModel $model)
    {
        $data =$request->param();
        $list = $model->getAccountEdit($id,$data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }
    /**结算账户----删除
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json
     */
    public function setAccountDel($id,Request $request,FinanceModel $model)
    {
        $list = $model->getAccountDel($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**结算账户----查找
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json
     */
    public function setAccountInfo($id,Request $request,FinanceModel $model)
    {
        $list = $model->getAccountInfo($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**账目类型----列表
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json
     */
    public function AccountType(Request $request,FinanceModel $model)
    {
        $list = $model->getAccountType();
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**账目类型----新增
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json
     */
    public function AccountTypeAdd(Request $request,FinanceModel $model)
    {
        $data =$request->param();
        $data['level'] =2;
        $list = $model->getAccountTypeAdd($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**账目类型----查找
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json
     */
    public function AccountTypeInfo($id,Request $request,FinanceModel $model)
    {
        $list = $model->getAccountTypeInfo($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }
    /**账目类型----修改
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json
     */
    public function AccountTypeEdit($id,Request $request,FinanceModel $model)
    {
        try {
            $data =$request->param();
            $list = $model->getAccountTypeEdit($id,$data);
            if (is_string($list)) {
                return $this->returnJson(500,$list);
            } else {
                return $this->returnJson(200,'ok');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }
    /**账目类型----删除
     * @param Request $request
     * @param FinanceModel $model
     * @return \think\response\Json
     */
    public function AccountTypeDel($id,Request $request,FinanceModel $model)
    {
        try {
            $list = $model->getAccountTypeDel($id);
            if (is_string($list)) {
                return $this->returnJson(500,$list);
            } else {
                return $this->returnJson(200,'ok');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }
}